/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 

using System;
using System.Collections;
using System.Text;
using System.Xml.Serialization;

namespace Madason.MetaModel
{
	public class Operation : ModelElement
	{
		private string returnTypeId;
		private Class @class;
		private ParameterList parameters;
        //added by hefeisla
        private bool isReturnTypeAList;

        //added by hefeisla
        public bool isList
        {
            get
            {
                if (isReturnTypeAList == null)
                    return false;
                else
                    return isReturnTypeAList;
            }
            set
            {
                isReturnTypeAList = value;
            }
        }

		[XmlAttribute("returnType")]
		public string ReturnTypeId
		{
			get { return returnTypeId; }
			set { returnTypeId = value; }
		}

		[XmlIgnore]
		public Class ReturnType
		{
			get { return Class.Model.FindClassById(ReturnTypeId); }
		}

		[XmlIgnore]
		public Class Class
		{
			get { return @class; }
			set { @class = value; }
		}

		[XmlElement(typeof(Parameter), ElementName = "parameter")]
		public ParameterList Parameters
		{
			get 
			{
				if (parameters == null)
				{
					parameters = new ParameterList();
					parameters.AfterParameterAdded += new ParameterAddDelegate(parameters_AfterParameterAdded);
				}
				return parameters; 
			}
			set 
			{ 
				parameters = value;
				parameters.AfterParameterAdded += new ParameterAddDelegate(parameters_AfterParameterAdded);
			}
		}

		void parameters_AfterParameterAdded(Parameter ns)
		{
			ns.Operation = this;
		}

		public string ParameterString
		{
			get
			{
				StringBuilder buffer = new StringBuilder();
				bool first = true;

				foreach (Parameter param in Parameters)
				{
					if (!first) buffer.Append(", ");
					buffer.Append(param.Type.ExplicitName);
					buffer.Append(" ");
					buffer.Append(param.Name);
					first = false;
				}
				return buffer.ToString();
			}
		}
	}

	#region Helper Types

	public delegate void OperationAddDelegate(Operation ns);


	/// <summary>
	/// list of operations
	/// </summary>
	public class OperationList : ArrayList
	{
		public event OperationAddDelegate AfterOperationAdded;


		/// <summary>
		/// add operation
		/// </summary>
		/// <param name="at"></param>
		public void Add(Operation at)
		{
			base.Add(at);
			if (AfterOperationAdded != null)
				AfterOperationAdded(at);
		}


		/// <summary>
		/// get operation by index
		/// </summary>
		/// <param name="i"></param>
		/// <returns></returns>
		public Operation this[int i]
		{
			get
			{
				return base[i] as Operation;
			}
			set
			{
				base[i] = value;
			}
		}

		/// <summary>
		/// get operation by his name
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public Operation this[string name]
		{
			get
			{
				foreach (Operation o in this)
					if (o.Name == name) return o;
				return null;
			}
		}
	}

	#endregion

}
